package com.ljxt.dt.web.servlet.basicinformationservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.GoodsInfo;
import com.ljxt.dt.domain.SupplierType;
import com.ljxt.dt.service.basicinformationservice.SupplierListService;
import com.ljxt.dt.service.basicinformationservice.impl.SupplierListServiceImpl;
import com.ljxt.dt.vo.basicInformation.GoodsInfoVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 糖糖
 * @version 1.0.0
 * @date 2020/5/17 20:09
 * @copyright 老九学堂
 */
@WebServlet("/updateSupplier")
public class UpdateSupplier extends HttpServlet {

    private SupplierListService supplierListService = new SupplierListServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("修改供应商的get请求");
        Map<String,Object> map = new HashMap<>();
        //查询供应商的类型
        List<SupplierType> supplierTypeList = supplierListService.selectSupplierType();
        map.put("typeList", supplierTypeList);
        //获取供应商主键编号
        String id = req.getParameter("id");
        map.put("id", id);
        //根据供应商编号查找对应的供应商物品
        List<GoodsInfoVO> goodsInfoList =  supplierListService.findSupplierGoods(Integer.parseInt(id));
        ObjectMapper mapper = new ObjectMapper();
        String goodsInfo = mapper.writeValueAsString(goodsInfoList);
        map.put("goodsInfoList",goodsInfo);
        //供应商类型名称
        String supplierType = req.getParameter("supplierType");
        System.out.println("supplierType="+supplierType);
        map.put("supplierType", supplierType);

        //供应商名字
        String supplierName = req.getParameter("supplierName");
        map.put("supplierName", supplierName);
        //供应商详细地址
        String address = req.getParameter("address");
        //以 - 拆分成字符串数组
        String[] str = address.split("-");

        //省名
        String pro = str[0];
        map.put("pro", pro);
        //市名
        String city = str[1];
        map.put("city", city);

        //区名
        String dis = str[2];
        map.put("dis", dis);
        //详细地址
        String detailAddress = str[3];
        map.put("detailAddress", detailAddress);
        //联系人名
        String contactName = req.getParameter("contactName");
        map.put("contactName", contactName);
        //联系电话
        String phone = req.getParameter("phone");
        map.put("phone", phone);

        System.out.println(id + "-" + supplierType + "-" + supplierName + "-" + address + "-" + contactName + "-" + phone);
        req.setAttribute("map",map);
        req.getRequestDispatcher("WEB-INF/views/basicinformation/updateSupplier.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("修改供应商的post请求");
        //1.获取供应商基础参数
        String id = req.getParameter("id");
        String typeId = req.getParameter("typeId");
        System.out.println("typeId= "+ typeId);
        String supplierName = req.getParameter("supplierName");
        String pro = req.getParameter("pro");
        String city = req.getParameter("city");
        String dis = req.getParameter("district");
        String detailAddress = req.getParameter("detailAddress");
        //2.获取联系人基本信息
        String contactName = req.getParameter("contactName");
        String phone = req.getParameter("phone");
        //获取供应商物品信息
        String[] goodsIdArray = req.getParameterValues("goodsIdArray");
        //将地址拼接成完整地址
        String address = pro+"-"+city+"-"+dis+"-"+detailAddress;
        System.out.println(id + "-" + typeId + "-" + supplierName + "-" + address + "-" + contactName + "-" + phone);
        if(goodsIdArray == null || goodsIdArray.length == 0){
            System.out.println("长度为0");
        } else {
            System.out.println(goodsIdArray.length);
        }
        //调用方法实现供应商信息修改
        boolean success = supplierListService.updateSupplier(Integer.parseInt(id),Integer.parseInt(typeId), supplierName, address, contactName, phone, goodsIdArray);
        //System.out.println("success = " + success);
    }
}
